package com.atguigu.day01;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Ticket {
    public static void main(String[] args) {
        Resource1 resource1 = new Resource1();
        new Thread(() -> {
            for (int i = 0; i <50 ; i++) {
                resource1.M1();
            }
        },"窗口1").start();


        new Thread(() -> {
            for (int i = 0; i <50 ; i++) {
                resource1.M1();
            }
        },"窗口2").start();


        new Thread(() -> {
            for (int i = 0; i <50 ; i++) {
                resource1.M1();
            }
        },"窗口3").start();

    }
}





class Resource1{

    private int num=1;

    Lock lock = new ReentrantLock();
    Condition condition1=lock.newCondition();
    Condition condition2=lock.newCondition();
    Condition condition3=lock.newCondition();

//    public synchronized void M1(){
//        if (num<=50){
//            System.out.println(Thread.currentThread().getName()+"卖出了第"+(num++)+"张票");
//        }
//    }

    public synchronized void M1(){
        Lock lock = new ReentrantLock();

        lock.lock();
        try{
            if (num<=50){
           System.out.println(Thread.currentThread().getName()+"卖出了第"+(num++)+"张票");
        }
        }finally {
            lock.unlock();
        }
    }




}
